
#include "BackgroundManager.h"
#include "hRandom.h"
#include "TextureManager.h"
#include "SB.h"
#include "LevelManager.h"
#include "hMath.h"

BackgroundManager::BackgroundManager()
{
    
}

BackgroundManager::~BackgroundManager()
{
}

void BackgroundManager::init()
{
    m_rBackground = new RenderableEntity("bg_1", Vector2(0, 0));
    m_rBackground->setScale(Vector2(640, 15));
    m_rBackground->setAlpha(0.0f);
    
    setupClouds();
    
    m_oFadeIn = new TimeLineEffect(false, false);
    m_oFadeIn->addEffect(new FadeEffect(m_rBackground, 0.3f, false, false, 1.0f));
    for(int i=0; i<NUM_CLOUDS; i++)
    {
        m_oFadeIn->addEffect(new FadeEffect(m_rBgClouds[i], 0.3f, false, false, 0.0f, 1.0f));
    }
    
    m_oFadeOut = new TimeLineEffect(false, false);
    m_oFadeOut->addEffect(new FadeEffect(m_rBackground, 0.3f, false, false, 0.0f));
    for(int i=0; i<NUM_CLOUDS; i++)
    {
        m_oFadeOut->addEffect(new FadeEffect(m_rBgClouds[i], 0.3f, false, false, 1.0f, 0.0f));
    }
}

void BackgroundManager::initNew()
{
    // backgrounds
    m_iCurrentBackground = 0;
    m_fNextBackgroundCurrentWeight = 0.0f;
    m_fNextBackgroundWeight = 0.0f;
    m_iNumberOfBGs = 6;
    m_oBackgrounds[0] = new RenderableEntity("bg1", Vector2(0,0));
    m_oBackgrounds[1] = new RenderableEntity("bg2", Vector2(0,0));
    m_oBackgrounds[2] = new RenderableEntity("bg3", Vector2(0,0));
    m_oBackgrounds[3] = new RenderableEntity("bg4", Vector2(0,0));
    m_oBackgrounds[4] = new RenderableEntity("bg5", Vector2(0,0));
    m_oBackgrounds[5] = new RenderableEntity("bg6", Vector2(0,0));
    for(int i=0; i<m_iNumberOfBGs; ++i)
    {
        m_oBackgrounds[i]->setPosition(Vector2(320.0f, 480.0f));
        m_oBackgrounds[i]->setCentered();
        m_oBackgrounds[i]->setScale(Vector2(60.0f, 1.88f));
    }
}

void BackgroundManager::renderNew()
{
    m_fNextBackgroundCurrentWeight = hMath::blend(m_fNextBackgroundCurrentWeight, m_fNextBackgroundWeight, 0.5f);
    float fAlpha = 1 - m_fNextBackgroundCurrentWeight;
    m_oBackgrounds[m_iCurrentBackground]->setAlpha(fAlpha);
    int iNextBG = (m_iCurrentBackground + 1) % m_iNumberOfBGs;
    m_oBackgrounds[iNextBG]->setAlpha(1- fAlpha);
    m_oBackgrounds[m_iCurrentBackground]->render();
    m_oBackgrounds[iNextBG]->render();
}


void BackgroundManager::updateBGNew()
{
    // update bgs
    m_fNextBackgroundWeight += 0.2f;
    if (m_fNextBackgroundWeight > 1.1f)
    {
        m_iCurrentBackground = (m_iCurrentBackground + 1) % m_iNumberOfBGs;
        m_fNextBackgroundWeight = 0.0f;
        m_fNextBackgroundCurrentWeight = 0.0f;
    }
}

void BackgroundManager::restartVars()
{
    m_iCurrentBackground = 0;
    m_fNextBackgroundWeight = 0.0f;
    m_fNextBackgroundCurrentWeight = 0.0f;
}

void BackgroundManager::update(float dt)
{
    float fGameSpeed = LevelManager::instance().getGameSpeed();
    for(int i=0; i<NUM_CLOUDS; i++)
    {
        RenderableEntity* ent = m_rBgClouds[i];
        ent->update();

        if(ent->getPosition().x < -ent->getSize().x * ent->getScale().x * 0.5)
            relocateCloud(i);
        else
            ent->translate(Vector2(SB::dt * - 10 * fGameSpeed, 0));
    }
}

void BackgroundManager::render()
{    
    m_rBackground->render();
    
    for(int i=0; i<NUM_CLOUDS; i++)
    {
        m_rBgClouds[i]->render();
    }
}

void BackgroundManager::setupClouds()
{
    //init clouds
    m_rBgClouds[0] = new RenderableEntity("bg_cloud_06", Vector2(100, 850));
    m_rBgClouds[1] = new RenderableEntity("bg_cloud_06", Vector2(600, 700));
    m_rBgClouds[2] = new RenderableEntity("bg_cloud_06", Vector2(300, 550));

    for(int i=0; i<NUM_CLOUDS; i++)
    {
        m_rBgClouds[i]->setLiving(true);
        m_rBgClouds[i]->setCentered();
        float fScale = hRandom::getRandom(0.5f, 0.8f);
        m_rBgClouds[i]->setScale(Vector2(fScale, fScale));
        m_rBgClouds[i]->setFlipHorizontal(hRandom::getRandom() < 0.5f);
        
         m_rBgClouds[i]->setAlpha(0.0f);
    }
}

void BackgroundManager::relocateCloud(int _iIndex)
{
    RenderableEntity *ent = m_rBgClouds[_iIndex];
        
    int x = 650;//hRandom::getRandomInt(SB::SCREEN_WIDTH, SB::SCREEN_WIDTH * 1.1f);
    int y = ent->getPosition().y;// * hRandom::getRandom(0.9f, 1.1f);
    ent->setPosition(Vector2(x, y));
    //float fScale = hRandom::getRandom(0.9f, 1.3f);
    //ent->setScale(Vector2(fScale, fScale));
    ent->setFlipHorizontal(hRandom::getRandom() < 0.5f);
}

void BackgroundManager::fadeIn()
{
    m_oFadeIn->start(true);
}

void BackgroundManager::fadeOut()
{
    m_oFadeOut->start(true);
}
